<?php
session_start();

$institute_id = $_SESSION['institute_id'];
$user_id= $_SESSION['user_id'];
//echo "institute_id=$institute_id<bR>";
if(isset($_POST['delete']) && $_POST['delete'] !='')
{
	//echo $_POST['del_id']."<br>";
	//exit;
	$sqltext="delete from timecode where id=?";
	$params = array();
	array_push($params,sql_escape($_POST['del_id']));
	db_change_query($conn,$sqltext,$params);	
	//recheck timecode for this asset, get new timecodesha1
	$sqltext="select timecode, timecode_type, data from timecode where asset_id= ?";
	$params = array();
	array_push($params,sql_escape($asset_id));		
	$result = db_select_query($conn,$sqltext,$params);	
	$timecode_checksum='';
	$timecode_sha1='';
	unset($timecode_list);
	while($row = db_fetch_object($result))
	{
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;
		}			
		$timecode_list[] = $timecode.$timecode_type.$data;
	}
	if(isset($timecode_list) )
	{
		sort($timecode_list);
		for($i=0;$i<count($timecode_list); $i++)
		{
			$timecode_checksum .= $timecode_list[$i];
		}
	}
	
	//update asset table 
	$params = array();
	$sqltext="UPDATE asset SET ";
	$sqltext .="last_modified_datetime =?";
	array_push($params,date("Y-m-d H:i:s"));
	$sqltext .=",last_modified_by =?";
	array_push($params,sql_escape($user_id));
	if($timecode_checksum !='')
	{
		$timecode_sha1 = sha1($timecode_checksum);
		//	echo $timecode_sha1."<br>";
	}
	$sqltext .=",timecodeSHA1 =?";
	array_push($params,sql_escape($timecode_sha1));
	$sqltext .=" WHERE id=?";
	//echo $sqltext."<br>";
	array_push($params,sql_escape($asset_id));
	
	//print_r($params);
	db_change_query($conn,$sqltext,$params);
		
}
if(isset($_POST['save']) && $_POST['save'] !='')
{
	$timecode = $_POST['timecode'];
	$timecode_type = $_POST['timecode_type'];
	$data = $_POST['data'];
	$error_message ='';
	if(!isset($timecode) ||  $timecode=='')
	{
		$error_message .= "missing required field - timecode";
	}
	if($error_message  =='')
	{
		
		$sqltext="INSERT INTO timecode(asset_id, timecode, timecode_type, data) values( ?,?,?,?)";
		$params = array();
		array_push($params,sql_escape($asset_id));
		array_push($params,sql_escape($timecode));
		array_push($params,sql_escape($timecode_type));
		array_push($params,sql_escape($data));
		
		db_change_query($conn,$sqltext,$params);	
		$sqltext="select timecode, timecode_type, data from timecode where asset_id= ?";
		$params = array();
		array_push($params,sql_escape($asset_id));		
		$result = db_select_query($conn,$sqltext,$params);	
		$timecode_checksum='';
		$timecode_sha1='';
		unset($timecode_list);
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				$$key = $value;
			}			
			$timecode_list[] = $timecode.$timecode_type.$data;
		}
		if(isset($timecode_list) )
		{
			sort($timecode_list);
			for($i=0;$i<count($timecode_list); $i++)
			{
				$timecode_checksum .= $timecode_list[$i];
			}
		}		
		//update asset table 
		$params = array();
		$sqltext="UPDATE asset SET ";
		$sqltext .="last_modified_datetime =?";
		array_push($params,date("Y-m-d H:i:s"));
		$sqltext .=",last_modified_by =?";
		array_push($params,sql_escape($user_id));
		
		$timecode_sha1 = sha1($timecode_checksum);
		//	echo $timecode_sha1."<br>";
		$sqltext .=",timecodeSHA1 =?";
		array_push($params,sql_escape($timecode_sha1));
		$sqltext .=" WHERE id=?";
		array_push($params,sql_escape($asset_id));
		db_change_query($conn,$sqltext,$params);

	}
	else
	{
		$alert_message = $error_message;
	}
}
$timecode_area .="";
if($asset_id !='')
{
 	$asset_id= $_REQUEST['asset_id'];
	$sqltext="select timecode,timecode_type, data,id
										from timecode WHERE asset_id=?";
	$params = array();	
	array_push($params,sql_escape($asset_id));
	$result = db_select_query($conn,$sqltext,$params);
	$timecode_area .="<br /><b>List all timecode for this asset:<br/></b><br/><table border='1' cellpadding='5' cellspacing='1'><tr><th>Timecode</th><th>Timecode type</th><th>Data</th><th>&nbsp;</th></tr>";
	$has_flag= false;
	while($row = db_fetch_object($result))
	{
		$has_flag= true;
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;
		}	
		$timecode_area .="<tr><td>$timecode</td><td>$timecode_type</td><td>$data</td><td>
		<input type='submit' name='delete' value='Remove' onclick='this.form.del_id.value=".$id."' /></td></tr>";
	}
	if(!$has_flag)
	{
		$timecode_area .="<tr><td colspan='9'>No timecode found for this asset!</td></tr>";
	}
	$timecode_area .="</table>";
}

$timecode_area .="<input type='hidden' name='del_id' value=\"\" /><br/><input type='hidden' name='asset_id' value=\"$asset_id\" />
<b>Add new timecode:<br /></b><hr style='border-color:#999;'/><br/><table>";

$timecode_area .="
<tr><td>Timecode:</td><td> <input type='text' name='timecode' value='' size='80'  maxlength='250' /></td></tr>
<tr><td>Timecode Type:</td><td> <input type='text' name='timecode_type' value='' size='80'  maxlength='250' /></td></tr>
<tr><td valign='top'>Data:</td><td> <textarea name='data' cols='80' rows='5'></textarea></td></tr>
";
$timecode_area .="<tr><td colspan='2' align='right'><input type='submit' name='save' value='Add Timecode'></td></tr>";
$timecode_area .="
</table>";
?>